using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Nb.FactoryAssistant.DAL.Abstracts.Entities;
using Nb.FactoryAssistant.Service.Abstracts;
using Nb.FactoryAssistant.Service.Abstracts.Dto;
using Nb.FactoryAssistant.Service.Abstracts.Dto;
using TianShu;
using TianShu.Domain;
using TianShu.Domain.Repositories;
using TianShu.EventBus;
using TianShu.Instructure.Abstracts;
using TianShu.Service.Dtos;
using TianShu.Service.Services.Crud;
using TianShu.Uow;
namespace Nb.FactoryAssistant.Service
{
    /// <summary>
    /// 仓库类别服务实现类
    /// </summary>
    public class WmsWarehouseTypeServiceImpl : CrudServiceImpl<WmsWarehouseType, Guid, WmsWarehouseTypeListOutput, WmsWarehouseTypeCreateUpdateInput>, IWmsWarehouseTypeService
    {
        public readonly IRepository<WmsWarehouse, Guid> _warehouseRepository;
        /// <sumary>
        /// 仓库类别构造函数
        /// </sumary>
        public WmsWarehouseTypeServiceImpl(IRepository<WmsWarehouse, Guid> warehouseRepository)
        {
            _warehouseRepository = warehouseRepository;
        }


        public override async Task<PageResultDto<IEnumerable<WmsWarehouseTypeListOutput>>> GetUrlParamAsync(List<UrlParams> urlParams)
        {
            Expression<Func<WmsWarehouseType, bool>> whereCondition = e => true;
            if (urlParams.TryGetString("keyword", out string keyword))
            {
                whereCondition = e => e.Name.Contains(keyword) || e.Code.Contains(keyword);
            }

            var (entityList, count) = await EntityRepository.GetListAsync(urlParams, whereCondition);

            var result = ObjectMapper.Map<IEnumerable<WmsWarehouseType>, IEnumerable<WmsWarehouseTypeListOutput>>(entityList);

            return new PageResultDto<IEnumerable<WmsWarehouseTypeListOutput>>(result, count);

        }

        public override async Task<PageResultDto<WmsWarehouseTypeListOutput>> InsertAsync(WmsWarehouseTypeCreateUpdateInput dto)
        {
            var warehouseTypeEntity = await EntityRepository.FindAsync(e => e.Code == dto.Code);
            if (warehouseTypeEntity != null)
            {
                return new PageResultDto<WmsWarehouseTypeListOutput>("code不能重复");
            }
            return await base.InsertAsync(dto);
        }
        public override async Task<PageResultDto<WmsWarehouseTypeListOutput>> UpdateAsync(Guid id, WmsWarehouseTypeCreateUpdateInput dto)
        {
            var dbUpdateEntity = await EntityRepository.FindAsync(id);
            if (dbUpdateEntity == null)
            {
                return new PageResultDto<WmsWarehouseTypeListOutput>($"The '{id}' is not found.");
            }

            if (dto.Code != dbUpdateEntity.Code)
            {
                var dbCodeEntity = await EntityRepository.FindAsync(e => e.Code == dto.Code);
                if (dbCodeEntity != null)
                {
                    return new PageResultDto<WmsWarehouseTypeListOutput>($"code不能重复");
                }
            }

            ObjectMapper.Map(dto, dbUpdateEntity);
            dbUpdateEntity = await EntityRepository.UpdateAsync(dbUpdateEntity);
            var data = ObjectMapper.Map<WmsWarehouseType, WmsWarehouseTypeListOutput>(dbUpdateEntity);

            return new PageResultDto<WmsWarehouseTypeListOutput>(data);
        }

        public override async Task<PageResultDto<bool>> DeleteAsync(IEnumerable<Guid> ids)
        {
            var warehouseEntity =await _warehouseRepository.FindAsync(e => ids.Contains(e.WarehouseTypeId));
            if (warehouseEntity!=null)
            {
                return new PageResultDto<bool>($"该类型被仓库【{warehouseEntity.Name}】引用，无法删除");
            }
            return await base.DeleteAsync(ids);
        }
    }
}
